package com.xq.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xq.config.properties.ManageProperties;
import com.xq.config.properties.MusicProperties;
import com.xq.model.element.StationInfoEle;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * http 工具类
 *
 * @author xu
 * 2021/8/9 18:05
 */
public class HttpUtils {

    public static JSONArray sendGetToList(MusicProperties musicProperties, String params) throws IOException, URISyntaxException {
        CloseableHttpClient httpClient = getDefaultHttpClient();
        URI uri = new URI("http", musicProperties.getHost(), musicProperties.getApiPath(), params, "");
        HttpGet httpGet = new HttpGet(uri);
        // 执行请求
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

        HttpEntity entity = httpResponse.getEntity();
        String resData = EntityUtils.toString(entity);

        // 关闭
        httpResponse.close();
        httpClient.close();

        // 将结果转为JSON格式
        JSONObject restJson = JSONObject.parseObject(resData);
        //获取返回码
        String code = String.valueOf(restJson.get("returnCode"));
        System.out.println("返回码:" + code);
        // 当返回成功时
        if("0".equalsIgnoreCase(code)){
            // 只输出数据
            return (JSONArray) restJson.get("DS");
        }
        return null;
    }

    /**
     * 获取远程链接文件
     * @param url 完整路径
     * @return 返回文件里面的内容
     *
     * 示例：
     * R4107 261611 1073510
     * 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 12.5 25.5=
     * NNNN
     */
    public static String sendGetFile(String url) throws IOException {
        CloseableHttpClient httpClient = getDefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        // 执行请求
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        return EntityUtils.toString(entity);
    }


    public static <T> void sendPostJson(String url, String jsonStr) throws IOException {
        CloseableHttpClient httpClient = getDefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        // 设置请求头
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        //装填参数
        StringEntity stringEntity = new StringEntity(jsonStr, "utf-8");
        stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        //设置参数到请求对象中
        httpPost.setEntity(stringEntity);
        //发送请求
        httpClient.execute(httpPost);
    }

    private static CloseableHttpClient getDefaultHttpClient(){
        return HttpClients.createDefault();
    }
}
